package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    /**
     * 添加购物车
     * @param shoppingCartDTO
     */
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //判断当前加入带购物车中的商品是否已经存在了
        /*
        select * from shopping_cart where user_id = ? and setmeal_id = ?
        select * from shopping_cart where user_id = 1 and dish_id = 1 amd dish_flavor
        用动态SQL实现
         */
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);//只可能为null或为1个

        //如果已经存在，则数量加1
        if(list!= null && list.size() > 0){
            ShoppingCart cart = list.get(0);
            cart.setNumber(cart.getNumber()+1);//update shopping_cart set number = ? where id = ?
            shoppingCartMapper.updateNumberById(cart);
        }

        else{
            //如果不存在，则直接添加到购物车中
            //判断添加到购物车的菜品是菜品还是套餐
            Long dishId = shoppingCartDTO.getDishId();
            if(dishId!= null){
                //本次添加到购物车的是菜品
                Dish dish = dishMapper.getById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setImage(dish.getImage());
//                shoppingCart.setNumber(1);
//                shoppingCart.setCreateTime(LocalDateTime.now());
            }else{
                Long setmealId = shoppingCartDTO.getSetmealId();
                //本次添加到购物车的是套餐
                Setmeal setmeal = setmealMapper.getById(setmealId);
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setImage(setmeal.getImage());


            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);

        }

    }

    /**
     * 显示购物车列表
     * @return
     */
    public List<ShoppingCart> showShoppingCartList() {
        //获取到当前微信用户的id
        Long currentId = BaseContext.getCurrentId();
        ShoppingCart shoppingcart = ShoppingCart.builder().userId(currentId).build();
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingcart);
        return list;
    }

    /**
     * 请空购物车
     */
    public void cleanShoppingCart() {
        //获取到当前微信用户的id
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.deleteUserId(userId);

    }

    /**
     * 删除购物车中的某一项
     * @param shoppingCartdto
     */
    public void deleteOne(ShoppingCartDTO shoppingCartdto) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartdto,shoppingCart);
        //设置查询条件,查询当前登录用户的购物车数据
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //删除当前购物车数据
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        if(list!= null && list.size() > 0) {
            ShoppingCart cart = list.get(0);
            Integer number = cart.getNumber();
            if (number > 1) {
                //数量大于1,数量减1
                cart.setNumber(number - 1);
                shoppingCartMapper.updateNumberById(cart);
            } else {
                //数量等于1,删除当前数据
                shoppingCartMapper.deleteById(cart.getId());
            }
        }
    }
}
